/**
 * Copyright 2013 DJSearch Co.,Ltd. All rights reserved.
 * DTM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.djsearch;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;

/**
 * @author dingzhongchang
 * @version 1.0.0
 */
public class DJSearchApp extends Application {
	static DJSearchApp App;
	public LocationClient mLocationClient = null;
	public TextView mTv;
	public Vibrator mVibrator;
	// 百度MapAPI的管理类
	public BMapManager mBMapMan = null;
	public MyLocationListenner myListener = new MyLocationListenner();
	// 授权Key
	// 申请地址：http://developer.baidu.com/map/android-mobile-apply-key.htm
	public String mStrKey = "5A85CA7CB959EE8751EA9D7BB6F25962E0F31B0F";
	public boolean m_bKeyRight = true; // 授权Key正确，验证通过

	@Override
	public void onCreate() {
		// CrashHandler crashHandler = CrashHandler.getInstance();
		// // 注册crashHandler
		// crashHandler.init(getApplicationContext());
		App = this;
		initEngineManager(this);
		mLocationClient = new LocationClient(this);
		mLocationClient.registerLocationListener(myListener);
		super.onCreate();
	}

	public void initEngineManager(Context context) {
		if (mBMapMan == null) {
			mBMapMan = new BMapManager(context);
		}
		if (!mBMapMan.init(mStrKey, new MyGeneralListener())) {
			Toast.makeText(App.getApplicationContext(), "BMapManager  初始化错误!",
					Toast.LENGTH_LONG).show();
		}
	}

	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	static class MyGeneralListener implements MKGeneralListener {

		@Override
		public void onGetNetworkState(int iError) {
			if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
				Toast.makeText(App.getApplicationContext(), "您的网络出错啦！",
						Toast.LENGTH_LONG).show();
			} else if (iError == MKEvent.ERROR_NETWORK_DATA) {
				Toast.makeText(App.getApplicationContext(), "输入正确的检索条件！",
						Toast.LENGTH_LONG).show();
			}
			// ...
		}

		@Override
		public void onGetPermissionState(int iError) {
			if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
				// 授权Key错误：
				Toast.makeText(App.getApplicationContext(),
						"请在 DemoApplication.java文件输入正确的授权Key！",
						Toast.LENGTH_LONG).show();
				App.m_bKeyRight = false;
			}
		}
	}

	@Override
	// 建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
	public void onTerminate() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onTerminate();
	}

	/**
	 * 监听函数，又新位置的时候，格式化成字符串，输出到屏幕中
	 */
	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return;
			if (mTv != null && location.getAddrStr() != null) {
				Log.i("LOC", location.getAddrStr());
				String city = location.getCity();
				String addr = location.getAddrStr();
				mTv.setText(addr);
				mTv.postInvalidate();
				mLocationClient.stop();
				SharedPreferences.Editor sharedata = getSharedPreferences(
						"LOC", 0).edit();
				sharedata.putString("latitude",
						String.valueOf(location.getLatitude()));
				sharedata.putString("longitude",
						String.valueOf(location.getLongitude()));
				sharedata.putString("accuracy",
						String.valueOf(location.getRadius()));
				sharedata.putString("direction",
						String.valueOf(location.getDerect()));
				sharedata.putString("addr", location.getAddrStr());
				sharedata.putString("city", location.getCity());
//				sharedata.putString("addr", city);
//				sharedata.putString("addr", location.getStreet());
//				sharedata.putString("addr", location.getStreetNumber());
				sharedata.commit();
			}
		}

		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null) {
				return;
			}
		}
	}
}
